package cn.yangke.user.service.impl;

import cn.yangke.basic.constants.BaseConstant;
import cn.yangke.basic.dto.RegisterDto;
import cn.yangke.basic.exception.BusinessException;
import cn.yangke.basic.util.MD5Utils;
import cn.yangke.basic.util.StrUtils;
import cn.yangke.user.domain.Logininfo;
import cn.yangke.user.domain.User;
import cn.yangke.user.mapper.LogininfoMapper;
import cn.yangke.user.mapper.UserMapper;
import cn.yangke.user.service.IUserService;
import cn.yangke.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangke
 * @since 2022-06-16
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 手机号注册
     * @param dto
     */
    @Override
    public void phoneRegister(RegisterDto dto) {
        //手机号
        String phone = dto.getPhone();
        //短信验证码
        String smsCode = dto.getSmsCode();
        //密码
        String password = dto.getPassword();
        //确认密码
        String passwordRepeat = dto.getPasswordRepeat();
        //判断两次输入的密码是否一致
        if(!password.equals(passwordRepeat)){
            throw new BusinessException("两次输入的密码不一致,请重新输入!!");
        }
        //判断短信验证码
        String key = BaseConstant.RegisterConstants.REGISTER_SMS_PREFIX + phone;
        Object smsCodeTmp = redisTemplate.opsForValue().get(key);
        //如果查询Redis为null说明验证码已过期
        if (smsCodeTmp == null){
            throw new BusinessException("验证码已过期,请重新获取!!");
        }
        //如果查询Redis不为null则判断是否与前端输入一致
        //不一致则提示前端
        if(!smsCode.equalsIgnoreCase(smsCodeTmp.toString().split(":")[0])){
            throw new BusinessException("您输入的短信验证码有误,请重新输入正确的验证码!!");
        }

        //前端验证没问题了则保存数据至数据库
        User user = dto2User(dto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    /**
     * 保存用户的登录信息表
     * @param user
     * @return
     */
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //bean工具类的复制方法,字段名一致的就复制
        BeanUtils.copyProperties(user,logininfo);
        //设置type为前端用户
        logininfo.setType(1);
        return logininfo;
    }

    /**
     * 保存user表
     * @param dto
     * @return
     */
    private User dto2User(RegisterDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        //获取盐值加密前缀
        String salt = StrUtils.getComplexRandomString(32);
        //使用MD5加密 密码
        String md5Pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }

    @Override
    public void update(User user) {


        super.update(user);
    }
}
